﻿(function($) {
	$.Banner = {};
	
	$.Banner = {
		$elemImg : null,
		
		Init : function()
		{
			$("#btImage").click(function() {
				$.Banner.FileOpen();
			});
			$("#btBannerAdd").click(function() {
				$.Banner.Create();
			});
			
			$("#selBannerList").change(function() {
				$.Banner.GetList();
			});
			
			$("#selBannerList").change();
		},

		FileOpen : function()
		{
			$.manager.fileopen(1, "$.Banner.FileOpen_Callback");
		},
		
		FileOpen_Callback : function( fileurl )
		{
			$("#txtImageURL").val( fileurl );
		},
		
		
		FileOpen2 : function()
		{
			$.manager.fileopen(1, "$.Banner.FileOpen_Callback2");
		},
		
		FileOpen_Callback2 : function( fileurl )
		{
			$.Banner.$elemImg.val( fileurl );
		},
		
		Create : function()
		{
			var BannerID = $("#selBannerList").val();
			var ImageURL = $("#txtImageURL").val();
			var Link = $("#txtLink").val();
			var LinkType = $("#selLinkType").val();
			
			$("#divAdd").ajaxNet(
			"/manager/banner/bannerMethod.aspx?_vb=Create",
			{
				"BannerID" : BannerID,
				"ImageURL" : ImageURL,
				"Link" : Link,
				"CodeLinkType" : LinkType
			}, function(results, context)
			{
				alert('등록되었습니다.');
				$.Banner.GetList();
			});
		},
		
		Remove : function( $elem, SeqNo )
		{
			$elem.ajaxNet(
			"/manager/banner/bannerMethod.aspx?_vb=Remove",
			{
				"SeqNo" : SeqNo
			}, function(results, context)
			{
				alert('삭제되었습니다.');
				$.Banner.GetList();
			});
		},
		
		GetList : function()
		{
			var BannerID = $("#selBannerList").val();
			
			$("#m_BannerList").ajaxNet(
			"/manager/banner/bannerMethod.aspx?_vb=GetList",
			{
				"BannerID" : BannerID
			}, function(results, context)
			{
				$("#m_BannerList").html( results );
				$("#m_BannerList input.m_ImageUploader").click(function() {
					var $tr = $(this).closest("tr");
					$.Banner.$elemImg = $tr.find("input.m_ImageURL");
					$.Banner.FileOpen2();
				});
				
				$("#m_BannerList input.m_btModify").click(function() {
					var $tr = $(this).closest("tr");
					var data = $tr.tagdata();
					$tr.hide();

					$("#m_BannerList tr.m_BannerModify_" + data.SeqNo ).show();
					
				});
				
				$("#m_BannerList input.m_ModifyCancel").click(function() {
					var $tr = $(this).closest("tr");
					var data = $tr.tagdata();
					$tr.hide();

					$("#m_BannerList tr.m_BannerInfo_" + data.SeqNo ).show();
					
				});
				
				$("#m_BannerList input.m_ModifyConfirm").click(function() {
					var $tr = $(this).closest("tr");
					var data = $tr.tagdata();
					
					var ImageURL = $tr.find("input.m_ImageURL").val();
					var Link = $tr.find("input.m_Link").val();
					var LinkType = $tr.find("select.m_LinkType").val();
					
					$tr.hide();
					$("#m_BannerList tr.m_BannerInfo_" + data.SeqNo ).show();
					
					$tr.ajaxNet(
					"/manager/banner/bannerMethod.aspx?_vb=Modify",
					{
						"SeqNo" : data.SeqNo,
						"ImageURL" : ImageURL,
						"Link" : Link,
						"CodeLinkType" : LinkType
					}, function(results, context)
					{
						alert('수정되었습니다.');
						$.Banner.GetList();
					});
					
				});
				
				$("#m_BannerList input.m_btRemove").click(function() {
					var $tr = $(this).closest("tr");
					var data = $tr.tagdata();
					if ( confirm("정말로 삭제하시겠습니까?") ) {
						$.Banner.Remove( $tr, data.SeqNo );
					}
				});
			});
		}
	};
	
	$(function() {
		$.Banner.Init();
	});
})(jQuery);